﻿Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Public MustInherit Class Shape


    Protected M As Mesh

    Dim Shapes As New List(Of Shape)
    Dim Primitives As New List(Of Primitive)
    Public Buffer As VertexBuffer
    Public BufferCount As Integer
    Public Visible As Boolean = True

    Public Material As New Material

    Property Device As Device

    MustOverride Sub Create()

    Sub New()
        LocationMatrix = Matrix.Translation(0, 0, 0)
        Material.Ambient = Color.Red
        Material.Diffuse = Color.White
    End Sub

    Overridable Sub Draw()
        If Not Visible Then Exit Sub
        Dim o = Device.Transform.World
        Device.Transform.World = Matrix.Multiply(LocationMatrix, Device.Transform.World)
        Device.Material = Material
        M.DrawSubset(0)
        Device.Transform.World = o
    End Sub


    Sub Add(ByVal Primitive As Primitive)
        Primitives.Add(Primitive)
    End Sub

    Sub Add(ByVal Shape As Shape)
        Shapes.Add(Shape)
    End Sub

    Sub CreateVertexBuffer(ByVal Device)
        BufferCount = GetBufferCount()
        Buffer = New VertexBuffer(GetType(CustomVertex.PositionNormalColored), 3 * BufferCount, Device, 0, CustomVertex.PositionNormalColored.Format, Pool.Default)
        Dim Vertices As CustomVertex.PositionNormalColored() = CType(Buffer.Lock(0, 0), CustomVertex.PositionNormalColored())
        Dim i As Integer = 0
        For Each t In Primitives
            If t.p.Count >= 3 Then
                For a As Integer = 0 To t.p.Count - 3
                    'MakeTriangle(Vertices, i, t.p(0).c, t.p(0).x, t.p(0).y, t.p(0).z, t.p(a + 1).x, t.p(a + 1).y, t.p(a + 1).z, t.p(a + 2).x, t.p(a + 2).y, t.p(a + 2).z)
                    MakeTriangle(Vertices, i, t.p(0).c, t.p(0).x, t.p(0).y, t.p(0).z, t.p(a + 2).x, t.p(a + 2).y, t.p(a + 2).z, t.p(a + 1).x, t.p(a + 1).y, t.p(a + 1).z)
                Next
            End If
        Next
        Buffer.Unlock()
    End Sub

    Private Sub MakeTriangle(ByVal vertices() As CustomVertex.PositionNormalColored, ByRef i As Integer, ByVal clr As Color, _
        ByVal x0 As Single, ByVal y0 As Single, ByVal z0 As Single, _
        ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, _
        ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single)

        vertices(i).X = x0 / 1000
        vertices(i).Y = y0 / 1000
        vertices(i).Z = z0 / 1000
        vertices(i).Color = clr.ToArgb
        'vertices(i).Normal = New Vector3(1, 0, 1)
        i += 1

        vertices(i).X = x1 / 1000
        vertices(i).Y = y1 / 1000
        vertices(i).Z = z1 / 1000
        vertices(i).Color = clr.ToArgb
        'vertices(i).Normal = New Vector3(1, 0, 1)
        i += 1

        vertices(i).X = x2 / 1000
        vertices(i).Y = y2 / 1000
        vertices(i).Z = z2 / 1000
        vertices(i).Color = clr.ToArgb
        'vertices(i).Normal = New Vector3(1, 0, 1)
        i += 1
    End Sub

    Private Function GetBufferCount() As Integer
        BufferCount = 0
        For Each Shape In Shapes
            BufferCount += Shape.GetBufferCount
        Next
        BufferCount += (From p In Primitives Select cn = p.p.Count - 2).Sum * 2
        Return BufferCount
    End Function

    Protected LocationMatrix As Matrix

    Sub Offset(ByVal x As Single, ByVal y As Single, ByVal z As Single)
        LocationMatrix = Matrix.Translation(x, y, z)
        'For Each s In Shapes
        '    s.Offset(x, y, z)
        'Next
        'For Each p In Primitives
        '    p.Offset(x, y, z)
        'Next
    End Sub

End Class

